export default {
	data(){
		return {
			modal1: false,//对话框
			ids:[],
			list:[],
			filter:{
				page:1,
				rows:2,
				total:0,
				name:'',
				title:'',
				content:''
			},
			nodeId:null,
			newsId:null,
			formValidateTree:{
                type:1,
                title:'',
                parentId:null
            },
		}
	},
	methods:{
		changeCate(selection){
            this.formValidate.cateId = selection[0]._id;            
            this.$http({
                method:'post',
                url:`http://localhost:3000/${this.module}/list`,
                data:{cateId:this.formValidate.cateId}
            }).then((response) => {             
                this.list = response.data.rows;
                this.filter.total = response.data.total;
            })
        },
		getTree(type){
            this.$http({
                method:'GET',
                url:'http://localhost:3000/cate/list/'+type,
                data:this.formValidateTree
            }).then((response) => {             
                this.baseData = response.data;
            })
        },
		changePageSize(pageSize){
			this.filter.rows = pageSize;
			this.getData();			
		},
		changePage(page){
			this.filter.page = page;
			this.getData();
		},
		getData(){
			this.$http({
        		method:'post',
        		url:`http://localhost:3000/${this.module}/list`,
        		data:this.filter
        	}).then((response) => {
			  	this.list = response.data.rows;
			  	this.filter.total = response.data.total;
			})
		},
		handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.$Message.success('提交成功!');
                    this.modal1=false;
                    this.submitData();
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },
		addData(){//添加按钮
			if(this.module == 'comment'){
				this.formValidate.userId = '59cb4bde94887f36000d732e';
                this.formValidate.newsId=this.newsId;
			}else{
				this.nodeId = null;
			}
    		this.modal1=true;
    	},
		editData(id){//修改按钮
    		this.nodeId = id;
    		this.$http({
        		method:'GET',
        		url:`http://localhost:3000/${this.module}/data/`+this.nodeId
        	}).then((response)=>{
        		//console.log(response);
        		this.formValidate = response.data;
        	});
        	this.modal1=true;
    	},
    	deleData(id){//删除单个
    		this.nodeId = id;
    		this.$http({
        		method:'delete',
        		url:`http://localhost:3000/${this.module}/data/`+this.nodeId
        	}).then((response)=>{
        		//location.reload();
        		this.reloadModule();
        	});
    	},
    	getIds(selection){
    		for(var i in selection){
    			this.ids[i]=selection[i]._id;
    		}
    	},
    	deleSelectData(){//选中删除    		
    		this.$http({
        		method:'post',
        		url:`http://localhost:3000/${this.module}/removes/`+this.ids        		
        	}).then((response)=>{
        		this.reloadModule();
        	});
    	},
    	submitData(){
    		var url = null;
    		if(this.nodeId){
    			url = `http://localhost:3000/${this.module}/data/`+this.nodeId;
    		}else{
    			delete this.formValidate._id;
    			url = `http://localhost:3000/${this.module}/data`
    		}
    		this.$http({
        		method:'POST',
        		url:url,	
        		data:this.formValidate       		
        	});
            this.reloadModule();
    	},
    	search(){
    		this.getData();
    	},
        reloadModule(){
            if(this.module=='comment'){
                this.isnewsId();
            }else{
                this.getData();
            }
        },
        isnewsId(){
            if(getCookie('newsId')){
                this.newsId = getCookie('newsId');
                this.$http({
                    method:'post',
                    url:'http://localhost:3000/comment/list',
                    data:{newsId:this.newsId}
                }).then((response) => {             
                    this.list = response.data.rows;
                    this.filter.total = response.data.total;
                })
            }
        }

	},
	created(){
		this.reloadModule();
	}
}